package com.gongpingjialoan.activity.main;

import android.os.Handler;
import android.support.v4.app.Fragment;
import android.view.View;
import android.widget.AbsListView;

import com.gongpingjialoan.R;
import com.gongpingjialoan.bean.Monitor;
import com.gongpingjialoan.global.GPJApplication;
import com.gongpingjialoan.utility.DhUtil;
import com.gongpingjialoan.utility.StepMonitor;
import com.gongpingjialoan.view.RLScrollView;
import com.handmark.pulltorefresh.library.PullToRefreshListView;

/**
 * Created by Administrator on 2016/8/9.
 */
public class BaseFragment extends Fragment {

    public String action = "";  //动作id

    public String type_action = "enter";  //动作类型

    public float roll_pages = 1.0f;  //滑屏

    //是否是滑动页面
    public boolean isScrollPage = false;

    private int scrollHeight = 0;

    private RLScrollView scrollView;

    private PullToRefreshListView mPullToRefreshListView;

    private View view;

    public Handler mHandler = new Handler();

    @Override
    public void onResume() {
        super.onResume();
        if (!isHidden())
            StepMonitor.getInstance().addMonitor(new Monitor(type_action, action, System.currentTimeMillis(), roll_pages));
    }


    //重置滑动距离
    public void reset() {
        roll_pages = 1.0f;
        scrollHeight = 0;
    }


    @Override
    public void onStart() {
        super.onStart();
        if (view != null) return;
        view = getView();
        if (view != null && isScrollPage) {
            try {
                scrollView = (RLScrollView) view.findViewById(R.id.scrollview);
                mPullToRefreshListView = (PullToRefreshListView) view.findViewById(R.id.listview);
            } catch (Exception e) {
            }
            if (scrollView != null) {
                registerScrollView();
            }
            if (mPullToRefreshListView != null) {
                registerListView();
            }
        }
    }


    private void registerScrollView() {
        scrollView.setOnScrollListener(new RLScrollView.OnScrollListener() {
            @Override
            public void onScrollChanged(int x, int y, int oldX, int oldY) {
            }

            @Override
            public void onScrollStopped() {
                int height = scrollView.getScrollY();
                //记录最高的滑动距离
                if (height >= scrollHeight) {
                    scrollHeight = height;
                    roll_pages = DhUtil.getRollPages(GPJApplication.getInstance(), scrollHeight);
                }
            }

            @Override
            public void onScrolling() {
            }
        });

    }

    private void registerListView() {
        mPullToRefreshListView.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                if (scrollState == SCROLL_STATE_IDLE) {
                    int height = getScrollY();
                    //记录最高的滑动距离
                    if (height >= scrollHeight) {
                        scrollHeight = height;
                        roll_pages = DhUtil.getRollPages(GPJApplication.getInstance(), scrollHeight);
                    }
                }
            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

            }
        });

    }

    public int getScrollY() {
        View c = mPullToRefreshListView.getRefreshableView().getChildAt(0);
        if (c == null) {
            return 0;
        }
        int firstVisiblePosition = mPullToRefreshListView.getRefreshableView().getFirstVisiblePosition();
        int top = c.getTop();
        return -top + firstVisiblePosition * c.getHeight();
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        mHandler.removeCallbacksAndMessages(null);
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
            StepMonitor.getInstance().addMonitor(new Monitor(type_action, action, System.currentTimeMillis(), roll_pages));
        }
    }
}
